package com.bwei.like.mynewstop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.like.mynewstop.Bean.NewsCategoryRoot;
import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.activity.CityListActivity;
import com.bwei.like.mynewstop.activity.WebView_activity;
import com.bwei.like.mynewstop.adapater.NewsCategoryAdapater;
import com.bwei.like.mynewstop.base.BaseData;
import com.bwei.like.mynewstop.base.BaseFragment;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.utils.URLUtils;
import com.bwei.like.mynewstop.view.MyListView;
import com.google.gson.Gson;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.xutils.common.util.LogUtil;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiKe on 2016/11/14.
 */
public class LocalCityFragment extends BaseFragment implements SpringView.OnFreshListener, View.OnClickListener {

    private NewsCategoryAdapater adapater;
    private ListView listView;
    public TextView tv_fragment_selectcity;
    private SpringView springView;
    public static final int REQUESTCODE = 100;
    private View view;
    private String cityName;
    private ArrayList<NewsCategoryRoot.DataBean> dataBeanArrayList = new ArrayList<>();

    @Override
    public View initView(LayoutInflater inflater) {
        view = CommonUtils.inflter(R.layout.fragment_localcity);
        initInfoView();
        return view;
    }

    //初始化控件
    private void initInfoView() {
        tv_fragment_selectcity = (TextView) view.findViewById(R.id.tv_fragment_selectcity);
        tv_fragment_selectcity.setOnClickListener(this);

        listView = (ListView) view.findViewById(R.id.listview_fragment_localcity);

        springView = (SpringView) view.findViewById(R.id.springView);
        //设置下拉刷新，上拉加载
        springView.setListener(this);
        //设置springView默认头和尾
        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        //设置springView头部隐藏
        springView.setType(SpringView.Type.FOLLOW);

        //监听，点击变色
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击变颜色
                CommonUtils.saveBoolean(dataBeanArrayList.get(position - 1).getTitle(), true);
                adapater.notifyDataSetChanged();
                //点击条目跳转界面
                Intent intent = new Intent(getActivity(), WebView_activity.class);
                intent.putExtra("DataBean", dataBeanArrayList.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initActivity() {

        String city = CommonUtils.getSp("cityName");
        if (city != null) {
            tv_fragment_selectcity.setText("当前城市:" + city + ",点击切换城市");
        }

        initData(URLUtils.URL_CATEGROY_PATH + "news_local" + "&user_city=" + city, BaseData.LONGTTIME, true);
    }


    private void initData(String requestPath, long requestTime, final boolean flag) {
        BaseData baseData = new BaseData() {
            @Override
            public void setResultData(String path) {
                LogUtil.i("+++++++++path"+path);
                lode();
                if (!TextUtils.isEmpty(path)) {
                    Gson gson = new Gson();
                    NewsCategoryRoot newsCategoryRoot = gson.fromJson(path, NewsCategoryRoot.class);
                    LogUtil.i("bean++++++"+newsCategoryRoot.getData().size());
                    if (flag) {
                        for (int i = 0; i < newsCategoryRoot.getData().size(); i++) {
                            if (!dataBeanArrayList.contains(newsCategoryRoot.getData().get(i))) {
                                dataBeanArrayList.add(0, newsCategoryRoot.getData().get(i));
                            }
                        }
                    } else {
                        for (int i = 0; i < newsCategoryRoot.getData().size(); i++) {
                            if (!dataBeanArrayList.contains(newsCategoryRoot.getData().get(i))) {
                                dataBeanArrayList.add(newsCategoryRoot.getData().get(i));
                            }
                        }
                    }
                    // ArrayList<NewsCategoryRoot.DataBean> data = (ArrayList<NewsCategoryRoot.DataBean>) newsCategoryRoot.getData();
                    if (adapater == null) {
                        adapater = new NewsCategoryAdapater(dataBeanArrayList, getActivity());
                        listView.setAdapter(adapater);
                    } else {
                        adapater.notifyDataSetChanged();
                    }

                }
            }

            @Override
            protected void setFailResult(int error_Net) {

            }
        };
        baseData.getData(requestPath, requestTime);
    }


    @Override
    public void onStop() {
        super.onStop();
        adapater = null;
    }

    /**
     * 获取城市名称
     */
    public void getCityName(String cityName) {
        this.cityName = cityName;
        CommonUtils.saveSp("cityName", cityName);
        tv_fragment_selectcity.setText("当前城市:" + cityName + ",点击切换城市");
        initData(URLUtils.URL_CATEGROY_PATH + "news_local" + "&user_city=" + cityName, BaseData.LONGTTIME, true);
    }

    //停止刷新
    public void lode() {
        springView.scrollTo(0, 0);
    }

    //下拉刷新，上拉加载
    @Override
    public void onRefresh() {
    //    initData(URLUtils.URL_CATEGROY_PATH + "news_local" + "&user_city=" + cityName + "&bd_loc_time=" + (System.currentTimeMillis() / 1000), BaseData.NOTIME, true);
    }

    @Override
    public void onLoadmore() {
     //   initData(URLUtils.URL_CATEGROY_PATH + "news_local" + "&user_city=" + cityName + "&max_behot_time=" + (System.currentTimeMillis() / 1000), BaseData.NOTIME, true);
    }

    //点击切换城市监听事件
    @Override
    public void onClick(View v) {
        //点击跳转城市列表界面
        Intent intent = new Intent(getActivity(), CityListActivity.class);
        getActivity().startActivityForResult(intent, REQUESTCODE);
    }

}
